#include <bits/stdc++.h>
using namespace std;
int n, a[2005], dp[2005][2005], cal[2005][2005];
void solve() {
    cin >> n;
    a[0] = -1;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            dp[i][j] = 0;
        }
    }
    for (int i = 0; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            cal[i][j] = 0;
        }
        cal[i][i] = 0;
        for (int j = i + 1; j <= n; j++) {
            cal[i][j] = cal[i][j - 1] + a[j] * (a[j] == a[j - 1]);
        }
    }
    for (int i = 1; i <= n; i++) {
        dp[i][0] = cal[1][i];
    }
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            for (int k = 0; k < j; k++) {
                    /*
                if (i == j + 1) {
                    dp[i][j] = max(dp[i][j], dp[j][k] + cal[j + 1][i]);
                } else {

                }
                */
                dp[i][j] = max(dp[i][j], dp[j][k] + cal[j + 1][i] + a[k] * (a[j + 1] == a[k]));
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << "\n";
}
signed main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
